package com.yunfei.springbootgoods.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunfei.springbootgoods.dto.Page;
import com.yunfei.springbootgoods.entity.Goods;
import com.yunfei.springbootgoods.entity.ShoppingCar;
import com.yunfei.springbootgoods.mapper.ShoppingCarMapper;
import com.yunfei.springbootgoods.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ShoppingCarServiceImpl implements ShoppingCarService {

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Override
    public void addShoppingCar(Integer goodsId) {
        
        ShoppingCar shoppingCar = new ShoppingCar();
        shoppingCar.setGoodsId(goodsId);
        shoppingCar.setAddTime(new Date());
        shoppingCarMapper.addShoppingCar(shoppingCar);

    }

    @Override
    public void deleteShoppingCar(Integer carId) {
        shoppingCarMapper.deleteShoppingCar(carId);
    }

    @Override
    public ShoppingCar getShoppingCarById(Integer carId) {
        ShoppingCar shoppingCarById = shoppingCarMapper.getShoppingCarById(carId);
        return shoppingCarById;
    }

    @Override
    public Page getShoppingCarPage(Integer pageNo, Integer pageSize) {
        Page page = new Page();

        PageHelper.startPage(pageNo,pageSize);
        List<ShoppingCar> shoppingList = shoppingCarMapper.getShoppingList();
        PageInfo<ShoppingCar> info = new PageInfo<>(shoppingList);

        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setCount((int) info.getTotal());
        page.setPageCount(info.getPages());
        page.setData(info.getList());
        return page;
    }
}
